//
//  BMWTabber.m
//  ShareCode
//
//  Created by erice on 15/10/31.
//  Copyright © 2015年 erice. All rights reserved.
//

#import "BMWTabber.h"

@interface BMWTabber ()<UIScrollViewDelegate>
{
    UIImageView *_bottomImageView;
}
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation BMWTabber

- (instancetype)initWithFrame:(CGRect)frame tabbernames:(NSArray*)namesArr withViews:(NSArray *)views{
    self =[super initWithFrame:frame];
    if (self) {
        
        self.containsubviews=views;
        self.namesArr=namesArr;
        self.backgroundColor =DAY_BG_COLOR;
        self.buttons =[NSMutableArray arrayWithCapacity:3];
        NSInteger count = namesArr.count;
        CGFloat btnWidth = self.bounds.size.width / count;
        CGFloat btnHeight = k_buttonHeight;
        CGFloat lineHeight = 2.0;
        UIColor *textColor = NIGHT_LABEL_TEXTCOLOR;
        UIColor *selectedColor = DAY_SHITI_COLOR;
    
        for (int i = 0; i < namesArr.count; i++)
        {
            NSString *title = [namesArr objectNilAtIndex:i];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i;
            CGFloat x = (btnWidth) * i;
            btn.frame = CGRectMake(x, 0, btnWidth, btnHeight);
            [btn setTitle:title forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:18];
            btn.userInteractionEnabled=YES;
            [btn setTitleColor:textColor forState:UIControlStateNormal];
            [btn setTitleColor:selectedColor forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self.buttons addObject:btn];
            [self addSubview:btn];
            [self sendSubviewToBack:btn];
            if (i != namesArr.count - 1) {
                UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(btnWidth * (i + 1), 0, 0.5,btn.height)];
                line.tag = i+k_line_baseTag;
                line.backgroundColor=[UIColor blackColor];
                [self addSubview:line];
            }
        }
        UILabel *bottomLine = [[UILabel alloc] initWithFrame:CGRectMake(0,btnHeight - 0.5, self.width, 0.5)];
        bottomLine.tag = k_bottomLine_tag;
        bottomLine.backgroundColor = NIGHT_TIJIAO_COLOR;
        [self addSubview:bottomLine];
      
         UIButton *button1 = [self.buttons objectNilAtIndex:0];
        _bottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(button1.center.x - btnWidth / 2.0, button1.bottom - lineHeight, btnWidth, lineHeight)];
        _bottomImageView.backgroundColor = [UIColor clearColor];
        NSString *imageName = @"teaching_btn_line_sel";
        _bottomImageView.image = [UIImage imageNamed:imageName];
        [self addSubview:_bottomImageView];
        [self BM_createScroll];
    }
    return self;
}

+ (instancetype)tabberWithFrame:(CGRect)frame tabbernames:(NSArray*)namesArr withViews:(NSArray *)views{

    return [[self alloc] initWithFrame:frame tabbernames:namesArr withViews:views];
}



- (void)BM_createScroll{
   
    if (self.namesArr.count !=self.containsubviews.count) {
     
       
        if (IOS8) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"BMW友情提醒！" message:@"您填写的按钮数与视图数不一致，请仔细检查代码" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
            [alertController addAction:okAction];
            [[self traverseResponderChainForUIViewController] presentViewController:alertController animated:YES completion:nil];
            
        }else{
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"BMW友情提醒！" message:@"您填写的按钮数与视图数不一致，请仔细检查代码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alert show];
        }
       
    }else {
        self.scroll =[[UIScrollView alloc]initWithFrame:CGRectMake(0,k_buttonHeight, screen_width,self.height-k_buttonHeight)];
        self.scroll.contentOffset=CGPointZero;
        self.scroll.contentSize=CGSizeMake(screen_width*self.namesArr.count, 0);
        self.scroll.showsHorizontalScrollIndicator =NO;
        self.scroll.showsVerticalScrollIndicator =NO;
        self.scroll.delegate =self;
        self.scroll.pagingEnabled =YES;
        self.scroll.bounces =NO;
        self.scroll.scrollsToTop =NO;
        [self addSubview:self.scroll];
        for (NSInteger i =0; i<self.containsubviews.count; i++) {
            if (!self.containsubviews) {
                UIView *temp =[[UIView alloc]initWithFrame:CGRectMake(0,0,screen_width,self.scroll.height)];
                NSArray *cls =@[[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor]];
                temp.backgroundColor =cls[i];
                [self.scroll addSubview:temp];
            }else {
                UIView *temp = self.containsubviews[i];
                temp.frame =(CGRect){{screen_width*i, 0},self.scroll.frame.size};
                [self.scroll addSubview:temp];
                
            }
            
        }
        
    }

    
}


- (void)tabBarButtonClicked:(UIButton*)sender{
    UIButton *btn = sender;
    [self scrollToIndex:btn.tag];
}

- (void)selectTabAtIndex:(NSInteger)index
{
    self.selectIndex = index;
    for (int i = 0; i < [self.buttons count]; i++)
    {
        UIButton *b = [self.buttons objectNilAtIndex:i];
        b.selected = NO;
        b.userInteractionEnabled = YES;
    }
    UIButton *btn = [self.buttons objectNilAtIndex:index];
    btn.selected = YES;
    btn.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.05 animations:^{
        CGRect rect = _bottomImageView.frame;
        rect.origin.x = btn.center.x - btn.width / 2.0;
        _bottomImageView.frame = rect;
    }];
}

#pragma mark - scroll
- (void)scrollToIndex:(NSInteger)index{
    if (index>self.namesArr.count) {
        return;
    }else{
        [self.scroll setContentOffset:CGPointMake(index*screen_width, 0) animated:YES];
        [self selectTabAtIndex:index];
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int row_index = (scrollView.contentOffset.x + screen_width /self.containsubviews.count) / screen_width;
    [self scrollToIndex:row_index];
    
  
}


@end
